payment.admin = liwe.module ( 'payment.admin', null, "Pagamenti" );

payment.admin._dest_div = "system-content";
payment.admin._visiblecnt = {};
payment._user_opts=null;
payment._module_opts=null;
payment._mode_opts = [ { 'label' : 'PayPal', 'name' : 'mode', 'value' : 'PP' }, 
					   { 'label' : 'Bonifico', 'name' : 'mode', 'value' : 'BB' },
					   { 'label' : 'A vista', 'name' : 'mode', 'value' : 'AV' }
					];
//{{{payment.admin._init = function ( dest_div )
payment.admin._init = function ( dest_div )
{
    if ( dest_div ) payment.admin._dest_div =  dest_div;
    var _acts = [
        { label: 'Pagamenti', func: function () { payment.admin._load_section( 'payment' );} }
    	]; 
    payment.admin.register ( _acts );
    payment.admin._am_payment_init ( 'payment_list' );
	payment.admin._set_module_opts();
};
//}}}
//{{{payment.admin._set_user_opts = function ( cback )
payment.admin._set_user_opts = function ( cback )
{
	payment._user_opts = []
	var _post = { 'action' : 'user.admin.list' };
	payment.ajax ( _post, function ( _resp )
	{
		_resp.users.iterate ( function ( item )
		{
			if ( item [ 'enabled' ] == 1 )
			payment._user_opts.push ( { 'label' : item [ 'login' ], 'value' : item  [ 'uid' ] } );
		});
		if ( cback ) cback();
	} ); 
};
//}}}
//{{{payment.admin._set_module_opts = function ( cback )
payment.admin._set_module_opts = function ( cback )
{
	payment._module_opts = []
	var _modules = system.admin.get_modules();
	_modules.iterate ( function ( item )
	{
		var _name = item [ 'module_name' ].split  ( '.' );
		payment._module_opts.push ( { 'label' : _name [ 0 ], 'value' : _name [ 0 ] } );
	});
	if ( cback ) cback();
};
//}}}
//{{{payment.admin._load_section = function ( section )
payment.admin._load_section = function ( section )
{
	
    payment.admin._visiblecnt = { payment : '' };
    var _func = payment.admin [ "_render_" + section ];
    _func ();
};  
//}}} 
//{{{payment.admin._show_cnt = function ( cnt_to_show, section )
payment.admin._show_cnt = function ( cnt_to_show, section )
{
    var _el;
    if ( payment.admin._visiblecnt [ section ] != '' )
    { 
        liwe.fx.fade_out ( payment.admin._visiblecnt [ section ] );
        _el = $el ( payment.admin._visiblecnt [ section ] );
        _el.style.display = "none";
    };
    _el = $el ( cnt_to_show );
    _el.style.display = "block";
    liwe.fx.fade_in ( cnt_to_show );
    payment.admin._visiblecnt [ section ] = cnt_to_show;
};
//}}}
//{{{PAYMENT
//{{{payment.admin._render_payment =  function()
payment.admin._render_payment =  function()
{
	var _dct, _admin_title;
	var _admin_title = String.formatDict ( payment.templates [ 'ADMIN_TITLE' ], { '_title' : 'GESTIONE PAGAMENTI' } ); 

	_dct = { '_admin_title' : _admin_title, '_list_title' : 'Elenco Pagamenti' , '_form_title' : 'Inserimento/modifica Pagamenti'  };

	$(	payment.admin._dest_div, String.formatDict ( payment.templates [ 'ADMIN_PAYMENT_LAYOUT' ], _dct ) );
	payment.admin._payment_refresh ();

};
//}}}
//{{{payment.admin._am_payment_init = function ( dest_div )
payment.admin._am_payment_init = function ( dest_div )
{
        if ( payment.admin.am_payment ) return;
        payment.admin.am_payment = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'am_payment',
                                        'list_action': "payment.ajax.list_payment",
                                        'mod_menu_actions': [
                                                                 { func: "payment.admin._payment_form();", 'class' : 'add_16' , label: 'Aggiungi' }
										                    ]
                                } );

        payment.admin.am_payment.set_row_fields (
                                [
                                        [ 'ID', 'id' ],
										[ 'Modulo', 'module'],
										[ 'Versamento' , '_payment_value'],
										[ 'Data Pagamento' , '_payment_date' ],
										[ 'Modalit&agrave; Versamento' , 'mode' ],
                                        [ 'Data Inserimento', '_created' ]
                                ]
                                );

        payment.admin.am_payment.cbacks [ 'row_manip' ] = payment.admin._rwman_payment;
        payment.admin.am_payment.cbacks [ 'del_item' ]  = function ( id ) { payment.admin.del_payment ( id ); };
        payment.admin.am_payment.cbacks [ 'edit_item' ]  = function ( id ) { payment.admin.edit_payment ( id ); };
        payment.admin.am_payment.cbacks [ 'noresult' ] = function () { payment.admin._payment_form(); };
        payment.admin.am_payment.cbacks [ 'show_results' ] = function () { payment.admin._show_cnt ( 'payment_panel_list', 'payment' ); };
};
//}}}
//{{{payment.admin._rwman_payment = function ( ds, row )
payment.admin._rwman_payment = function ( ds, row )
{
	row [ "_id" ] = row [ 'id' ];
	row [ "_created" ] = Date.formatted ( "%d %B %Y", row [ 'created' ].split( " ") [ 0 ]); 
	row [ "_payment_date" ] = Date.formatted ( "%d %B %Y", row [ 'payment_date' ].split( " ") [ 0 ]); 
	var _dct = { '_class' : 'money' };
	_dct [ '_txt' ] ='&euro; ' +  Money.fromLongInt ( row [ 'payment_value' ] ) ;
	row [ "_payment_value" ] = String.formatDict ( system.templates [ 'admin_man_col' ] , _dct );
};
//}}}
//{{{payment.admin._payment_refresh =  function ( cback )
payment.admin._payment_refresh =  function ( cback )
{
	payment.admin.am_payment.show_list ();
	if ( cback ) cback();
};
//}}}
//{{{payment.admin.edit_payment = function ( id )
payment.admin.edit_payment = function ( id )
{
        var _post = { action : 'payment.ajax.get_payment', id_payment : id };
        payment.ajax ( _post , function ( _resp )
        {
	        payment.admin._payment_form ( _resp.payment );
        });
};
//}}}
//{{{payment.admin._payment_save =  function ()
payment.admin._payment_save =  function ()
{
    liwe.utils.notifier.show ( 'Salvato correttamente', 'info', 2000);
};
//}}}
//{{{payment.admin._payment_form =  function ( data )
payment.admin._payment_form =  function ( data )
{
	payment.admin._set_user_opts ( function ()
	{
		var dest_div = 'payment_form';
		if ( typeof ( data ) == 'undefined' ) data = {};
		var _f = new liwe.form.instance ( 'am_payment_form' );
		_f.events [ 'complete' ] = payment.admin._payment_save;
		_f.hidden ( "action", 'payment.admin.set_payment' );
		_f.hidden ( 'id_payment', data.get ( 'id', 0 )  );
		_f.select ( { 'label': 'Modulo', name: 'module', options: payment._module_opts, force_select: true, 'value' : data.get ( 'module' , 0 ) } );
		_f.select ( { 'label': 'Utente', name: 'uid', options: payment._user_opts, force_select: true, 'value' : data.get ( 'uid' , 0 ) } );
		_f.money ( { 'label' : 'Versamento', name : 'payment_value', size : 10, 'maxlength' : 10, 'value' : data.get ( 'payment_value', 0 ) } );
		_f.calendar ( { 'label' : 'Data pagemento', name: 'payment_date', time: false, 'value' : data.get ( 'payment_date', Date.formatted ( "%Y-%m-%d", Date() ) )  } );
		payment._mode_opts.iterate ( function ( opt )
		{
			_f.radio ( opt );
		} );
		_f.sep();
		_f.jconed ( { 'label' : 'Note', 'name' : "note", colspan : 3,  width: 600, height: 300, 'value' : data.get ( 'note' , '' ) } );
		_f.submit ( { value: 'Salva', 'class' : "button green",nonl : true, colspan : 2 } );
		_f.button ( { value: 'Indietro', 'class' : "grey", "onclick" : "payment.admin._payment_refresh()" } );
		_f.set ( dest_div );
		_f.set_value ( 'mode' , data.get ( 'mode', 'PP' ) );
		payment.admin._show_cnt ( 'payment_panel_form', 'payment' );
	} );

};
//}}}
//}}}
//{{{payment.admin.init = function ( dest_div )
payment.admin.init = function ( dest_div )
{
	if ( ! payment.templates )
	{
		payment.load_templates ( function () { payment.admin._init ( dest_div); } );
		return;
	}
	payment.admin._init ( dest_div );
};
//}}}
